/**
 * Created by wangxin on 2017-9-26 10:02:01.
 */

'use strict';
const page = {
    url: {
        loadEventRule: contextData.contextPath + '/work/loadEventRule',
        getCoupon: contextData.contextPath + '/work/getCoupon',
    },
    requestFlag: false,
};

page.init = function () {
    page.initElement();
    page.initEvent();
};

page.initElement = function () {
    const thisPage = this;
    Dolphin.ajax({
        url: thisPage.url.loadEventRule,
        type: "get",
        forceUrl: 'true',
        data: {eventId: contextData.data.eventId},
        onSuccess: function (data) {
            if(data.event.backgroundImg){
                $('#container').css('background-image', 'url('+Dolphin.path.mengkeUploadPath+data.event.backgroundImg+')');
            }else{
                $('#container').css('background-image', 'url('+Dolphin.path.mengkePublicPath+'/custom/images/prize/background2.png)');
            }
            if(data.event.couponImg){
                $('#coupon').attr('src', Dolphin.path.mengkeUploadPath+data.event.couponImg);
            }else{
                $('#coupon').attr('src', Dolphin.path.mengkePublicPath+'/custom/images/prize/coupon2.png');
            }
            document.title = data.event.eventName;

            var createOrgCode = contextData.userData && contextData.userData.createOrgCode || null;
            var store = data && data.store || null;
            if(!(!createOrgCode || !store || (store && !store.length) || store.find(o=>o.code === createOrgCode))){
                var bg = $("<div style='position:fixed;top: 0;bottom:0;left:0;right:0;text-align: center;padding-top:90%;font-weight:700;color: white;font-size:24px;background: rgba(55,55,55,0.75)'>您不能参加本次活动" +
                    "<br />请咨询相关门店人员</div>");
                $("body").append(bg);
            }

            $('#coupon').html('立即领取').click(function(){
                if(!thisPage.requestFlag){
                    thisPage.requestFlag = true;
                    Dolphin.ajax({
                        url:thisPage.url.getCoupon,
                        async: false,
                        data:{
                            prizeId:data.rule[0]._id,
                            gameType:'getCouponSingle',
                        },
                        success:function(reData){
                            thisPage.requestFlag = false;
                            $.alert(reData.message);
                        }
                    });
                }
            })


        },
        onError: function (e) {
            alert('网络出错啦，请您重新加载');
        }
    });
};

page.initEvent = function () {
};

page.formatterDate = function (val, formatter) {
    if(val){
        var date = Dolphin.string2date(val, "yyyy-MM-ddThh:mm:ss.");
        date.setHours(date.getHours() + 8);
        return Dolphin.date2string(date, formatter || "yyyy-MM-dd");
    }else{
        return '';
    }
};

$(function () {
    page.init();
});




